//
//  MoreChannelListCell.m
//  History
//
//  Created by kane on 16/9/10.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "MoreChannelListCell.h"

@implementation MoreChannelListCell
@synthesize iconIV,markBtn,titleLbl,descLbl,flagIV;
@synthesize channelInfo,delegate;

- (void)awakeFromNib {
    [super awakeFromNib];
    [markBtn drawCornerRadius:4];
    [markBtn drawBorderColor:[UIColor orangeColor] Width:.5];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setupData:(NSDictionary *)info index:(NSInteger)index
{
    channelInfo = info;
    if ([info[@"channel_icon"] convertToString].length > 0) {
        [iconIV setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",FileURL,info[@"channel_icon"]]]];
        titleLbl.text = [info[@"channel_name"] convertToString];
        descLbl.text = [info[@"channel_description"] convertToString];
        NSArray *ary = info[@"is_sub"];
        BOOL isSub = ary.count > 0;
        [markBtn drawBorderColor:isSub?[UIColor lightGrayColor]:[UIColor orangeColor] Width:.5];
        [flagIV setImage:[UIImage imageNamed:isSub?@"nike":@"Home_add"]];
        markBtn.selected = isSub;
    }    
}

- (IBAction)markBtnClick:(UIButton *)sender
{
    if (sender.selected) {
        NSArray *subAry = channelInfo[@"is_sub"];
        if (subAry.count > 0) {
            NSDictionary *item = subAry.firstObject;
            NSDictionary *params = @{@"cs_cid":[[UserEntity sharedUser] getUserID],@"cs_channelid":[channelInfo[@"channel_id"] convertToString],@"cs_id":[item[@"cs_id"] convertToString]};
            [WebService deleteMarkChannel:params WithBlock:^(id result, NSError *error) {
                if (!error) {
                    if ([result[@"errcode"] integerValue] == 0) {
                        markBtn.selected = NO;
                        [markBtn drawBorderColor:markBtn.selected?[UIColor lightGrayColor]:[UIColor orangeColor] Width:.5];
                        [flagIV setImage:[UIImage imageNamed:markBtn.selected?@"nike":@"Home_add"]];
                        if (delegate && [delegate respondsToSelector:@selector(sendValue:)]) {
                            [delegate sendValue:sender.selected];
                        }
                        NSDictionary *dict = @{@"channel_name":channelInfo[@"channel_name"],@"cs_channelid":channelInfo[@"channel_id"]};
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteUserchannel" object:dict];
                    }
                    
                }
                
            }];
        }
    }else {
        NSDictionary *params = @{@"cs_cid":[[UserEntity sharedUser] getUserID],@"cs_channelid":[channelInfo[@"channel_id"] convertToString]};
        [WebService addMarkChannel:params WithBlock:^(id result, NSError *error) {
            if (!error) {
                if ([result[@"errcode"] integerValue] == 0) {
                    sender.selected = YES;
                    [markBtn drawBorderColor:markBtn.selected?[UIColor lightGrayColor]:[UIColor orangeColor] Width:.5];
                    [flagIV setImage:[UIImage imageNamed:markBtn.selected?@"nike":@"Home_add"]];
                    if (delegate && [delegate respondsToSelector:@selector(sendValue:)]) {
                        [delegate sendValue:sender.selected];
                    }
                    NSDictionary *dict = @{@"channel_name":channelInfo[@"channel_name"],@"cs_channelid":channelInfo[@"channel_id"]};
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"addUserchannel" object:dict];
                }
            }
        }];
    }
}

@end
